package com.gable.report.controller;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForHospitalCostService;
import com.gable.report.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.getParamMap;


/**
 * 医院绩效
 *
 */
@Controller
@RequestMapping("charts/hospital/cost")
public class ChartsForHospitalCostController {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    ChartsForHospitalCostService chartsForHospitalCostService;

    @Autowired
    UserService userService;

    /**
     * 医院报表区间条件查询
     * @return
     */
    //2017-9-26 针对无绩效的医院报错处理 修改人：杨青桦
    @RequestMapping(value="/queryInterval",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryInterval(String userId, ChartsHospitalQuery chartsHospitalQuery, HttpServletRequest request){
        try {

            if(userId == null || "".equals(userId)){
                return new ReturnResultUtil(0,"用户id不能为空");
            }
            User user = userService.queryUserById(Long.parseLong(userId));
            if(user == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Long hospitalId = user.getHospitalId();
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            Map<String, Object> hospitalPerformance = chartsForHospitalCostService.queryHospitalPerformance(paramMap);
            if(hospitalPerformance != null){
                Double income = Double.parseDouble(hospitalPerformance.get("jnIncome").toString());    //今年收入
                Double expenditure =Double.parseDouble(hospitalPerformance.get("jnExpenditure").toString());   //今年支出
                //收入和支出都有
                if(income > 0 && expenditure > 0){
                    paramMap.put("type",2);
                }
                else
                //只有收入
                if(income > 0 && expenditure == 0){
                    paramMap.put("type",3);
                }
                else
                //只有支出
                if(income == 0 && expenditure > 0){
                    paramMap.put("type",4);
                }

            }
            //收入和支出都没有
            else{
                paramMap.put("type",2);
            }
            paramMap.put("id",hospitalId);
            return chartsForHospitalCostService.queryInterval(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }

    }
    /**
     * @Description: 绩效概况
     * @return
     */
    @RequestMapping(value="/overview",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryOverview(String userId, ChartsHospitalQuery chartsHospitalQuery, HttpServletRequest request){
        try {
            if(userId == null || "".equals(userId)){
                return new ReturnResultUtil(0,"用户id不能为空");
            }
            User user = userService.queryUserById(Long.parseLong(userId));
            if(user == null){
                return new ReturnResultUtil(0,"该用户不存在");
            }
            Long hospitalId = user.getHospitalId();
            return chartsForHospitalCostService.queryOverview(hospitalId, chartsHospitalQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"error");
        }
    }

    /**
     * 部门绩效报表
     * @return
     */
    @RequestMapping(value="/department",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDepartments(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryDepartments(hospitalId, chartsHospitalQuery);
    }


    /**
     * 设备类型绩效报表
     * @return
     */
    @RequestMapping(value="/type",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceTypes(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryDeviceTypes(hospitalId, chartsHospitalQuery);
    }

    /**
     * 品牌绩效报表
     * @return
     */
    @RequestMapping(value="/brand",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryBrand(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryBrand(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description:  设备型号
     * @return
     */
    @RequestMapping(value="/model",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceModels(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryDeviceModels(hospitalId, chartsHospitalQuery);
    }


    /**
     * @Description:  "➕"号查询设备绩效
     * @return
     */
    @RequestMapping(value="/device",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryDeviceInfo(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryAssets(hospitalId, chartsHospitalQuery);
    }

    /**
     * @Description: 月份数据
     * @return
     */
    @RequestMapping(value="/month-trend",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryForMonth(String userId,ChartsHospitalQuery chartsHospitalQuery){
        if(userId == null || "".equals(userId)){
            return new ReturnResultUtil(0,"用户id不能为空");
        }
        User user = userService.queryUserById(Long.parseLong(userId));
        if(user == null){
            return new ReturnResultUtil(0,"该用户不存在");
        }
        Long hospitalId = user.getHospitalId();
        return chartsForHospitalCostService.queryForMonth(hospitalId, chartsHospitalQuery);
    }




}
